﻿using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Unity;
using Autorent.Desktop.Main;
using Autorent.Desktop.Model.AutorentMngrServiceReference;
using Autorent.Desktop.Modules.Employees.Services;
using Autorent.Desktop.Modules.Employees.Views;
using Autorent.Desktop.Modules.Events;

namespace Autorent.Desktop.Modules.Employees.ViewModels
{
    public class EmployeesListViewModel : ViewModelBase
    {
        #region Public Properties

        public EmployeesListView View { get; set; }

        public ObservableCollection<Employee> Employees
        {
            get
            {
                return _repository.GetEmployees();
            }
        }

        public Employee SelectedEmployee { get; set; }

        #endregion

        #region Constructors

        public EmployeesListViewModel(EmployeesListView view, IEmployeesRepository repository, IUnityContainer container,  IRegionManager regionManager, IEventAggregator eventAggregator)
        {
            View = view;
            view.Model = this;

            _repository = repository;
            _container = container;
            _regionManager = regionManager;
            _eventAggregator = eventAggregator;
            _eventAggregator.GetEvent<CollectionChangedEvent>().Subscribe(new Action<string>(OnPropertyChanged));
        }

        #endregion

        #region Commands

        public ICommand CreateEmployee
        {
            get
            {
                return new RelayCommand(param => createEmployee());
            }
        }

        public ICommand UpdateEmployee
        {
            get
            {
                return new RelayCommand(param => updateEmployee());
            }
        }

        public ICommand DeleteEmployee
        {
            get
            {
                return new RelayCommand(param => deleteEmployee());
            }
        }

        public ICommand SearchEmployee
        {
            get
            {
                return new RelayCommand(param => searchEmployee());
            }
        }

        #endregion

        #region Private Variables

        private IEmployeesRepository _repository;
        private readonly IRegionManager _regionManager;
        private readonly IUnityContainer _container;
        private readonly IEventAggregator _eventAggregator;

        #endregion

        #region Private Methods

        private void createEmployee()
        {
            object view = _regionManager.Regions["DetailsRegion"].GetView("CreateEmployeeView");

            if (view == null) 
            {
                foreach (var views in _regionManager.Regions["DetailsRegion"].Views)
                {
                    _regionManager.Regions["DetailsRegion"].Remove(views);
                }

                _regionManager.Regions["DetailsRegion"].Add(_container.Resolve<CreateEmployeeViewModel>().View, "CreateEmployeeView"); 
            }
        }

        private void updateEmployee()
        {
            if (SelectedEmployee != null)
            {
                if (_regionManager.Regions["DetailsRegion"].GetView("UpdateEmployeeView") == null)
                {
                    foreach (var views in _regionManager.Regions["DetailsRegion"].Views)
                    {
                        _regionManager.Regions["DetailsRegion"].Remove(views);
                    }

                    _regionManager.Regions["DetailsRegion"].Add(_container.Resolve<UpdateEmployeeViewModel>(new ParameterOverrides() { { "employee", SelectedEmployee } }).View, "UpdateEmployeeView");
                }
            }
            else { MessageBox.Show("Select employee", "Information", MessageBoxButton.OK, MessageBoxImage.Information); }
        }

        private void deleteEmployee()
        {
            _repository.DeleteEmployee(SelectedEmployee);
            OnPropertyChanged("Employees");
        }

        private void searchEmployee()
        {
            if (_regionManager.Regions["SearchRegion"].GetView("SearchEmployeeView") == null)
            {
                _regionManager.Regions["SearchRegion"].Add(_container.Resolve<SearchEmployeeViewModel>().View, "SearchEmployeeView");
            }
        }

        #endregion
    }
}
